/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.rank;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.entity.setting.UserAbilityLabels;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;

import java.util.List;

public class UserActiveAdapter extends RecyclerView.Adapter<UserActiveAdapter.AbilityContentViewHolder> {
    private static int TIME_DURATION = 20;
    private static int TIME_INTERVAL = 18;
    private List<ActiveBean> mActiveBeans;
    public UserActiveAdapter(List<ActiveBean> mActiveBeans) {
        this.mActiveBeans=mActiveBeans;
    }

    @Override
    public AbilityContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_ability_item_content, parent, false);
        return new AbilityContentViewHolder(sView, viewType);
    }

    @Override
    public void onBindViewHolder(AbilityContentViewHolder holder, int position) {
        ActiveBean activeBean = mActiveBeans.get(position);
        holder.itemName.setText(activeBean.name);
        String percentFormat = holder.itemName.getContext().getResources().getText(R.string.user_grew_percent).toString();
        holder.itemPercent.setText(String.format(percentFormat, activeBean.uncount, activeBean.count));
        double current = MathUtils.parseStringToDouble(activeBean.uncount);
        double total = MathUtils.parseStringToDouble(activeBean.count);

        holder.ProgressBack.post(new Runnable() {
            @Override
            public void run() {
                int totalWidth = holder.ProgressBack.getWidth();
                double radius = 0;
                if (current != -1)
                    radius = current / total;
                int proTotalWidth = (int) (totalWidth * radius);
                int processWidth = holder.ProgressContent.getWidth();
                if (processWidth < proTotalWidth) {
                    new Handler().post(new PercentRunnable(proTotalWidth, processWidth, (proTotalWidth - processWidth) / TIME_DURATION, holder.ProgressContent));
                }
            }
        });

    }

    class PercentRunnable implements Runnable {
        private int total;
        private int source;
        private int interAdd;
        private View mView;

        public PercentRunnable(int total, int source, int interAdd, View mView) {
            this.total = total;
            this.source = source;
            this.interAdd = interAdd;
            this.mView = mView;
        }

        @Override
        public void run() {
            if (source < total) {
                source += interAdd;
                source = source > total ? total : source;
            } else
                return;
            if (source <= total) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mView.getLayoutParams();
                params.width = source;
                mView.setLayoutParams(params);
                new Handler().postDelayed(new PercentRunnable(total, source, interAdd, mView), TIME_INTERVAL);
            }
        }
    }


    @Override
    public int getItemCount() {
        return mActiveBeans.size();
    }


    public class AbilityContentViewHolder extends RecyclerView.ViewHolder {
        TextView itemName, itemPercent;
        ImageView ProgressBack, ProgressContent;


        public AbilityContentViewHolder(View view, int viewType) {
            super(view);
            itemName = (TextView) view.findViewById(R.id.line_text_title);
            itemPercent = (TextView) view.findViewById(R.id.progress_percent);
            ProgressBack = (ImageView) view.findViewById(R.id.ability_progress_back);
            ProgressContent = (ImageView) view.findViewById(R.id.ability_progress_content);
        }

    }


}
